home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libmpeg_src.lha / ordered.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-25  |  7.8 KB  |  288 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /* This file contains C code to implement an ordered dither. */
  22.  
  23. #include <config.h>
  24. #include "video.h"
  25. #include "proto.h"
  26. #include "dither.h"
  27.  
  28. #define DITH_SIZE 16
  29.  
  30.  
  31. /* Structures used to implement hybrid ordered dither/floyd-steinberg
  32.    dither algorithm.
  33. */
  34.  
  35. static unsigned char *l_darrays[DITH_SIZE];
  36. static unsigned char *cr_darrays[DITH_SIZE];
  37. static unsigned char *cb_darrays[DITH_SIZE];
  38.  
  39. /*
  40.  *--------------------------------------------------------------
  41.  *
  42.  *  InitOrderedDither--
  43.  *
  44.  *    Structures intialized for ordered dithering. 
  45.  *
  46.  * Results:
  47.  *    None.
  48.  *
  49.  * Side effects:
  50.  *      None.
  51.  *
  52.  *--------------------------------------------------------------
  53.  */
  54.  
  55. void
  56. InitOrderedDither()
  57. {
  58.   int i, j, k, err_range, threshval;
  59.   unsigned char *lmark, *cmark;
  60.  
  61.   for (i=0; i<DITH_SIZE; i++) {
  62.     lmark = l_darrays[i] = (unsigned char *) malloc(256);
  63.  
  64.     for (j=0; j<lum_values[0]; j++) {
  65.       *lmark++ = 0;
  66.     }
  67.  
  68.     for (j=0; j<(LUM_RANGE-1); j++) {
  69.       err_range = lum_values[j+1] - lum_values[j];
  70.       threshval = ((i * err_range) / DITH_SIZE)+lum_values[j];
  71.  
  72.       for (k=lum_values[j]; k<lum_values[j+1]; k++) {
  73.     if (k > threshval) *lmark++ = ((j+1) * (CR_RANGE * CB_RANGE));
  74.     else *lmark++ = (j * (CR_RANGE * CB_RANGE));
  75.       }
  76.     }
  77.  
  78.     for (j=lum_values[LUM_RANGE-1]; j<256; j++) {
  79.       *lmark++ = (LUM_RANGE-1)*(CR_RANGE * CB_RANGE);
  80.     }
  81.   }
  82.  
  83.   for (i=0; i<DITH_SIZE; i++) {
  84.     cmark = cr_darrays[i] = (unsigned char *) malloc(256);
  85.  
  86.     for (j=0; j<cr_values[0]; j++) {
  87.       *cmark++ = 0;
  88.     }
  89.  
  90.     for (j=0; j<(CR_RANGE-1); j++) {
  91.       err_range = cr_values[j+1] - cr_values[j];
  92.       threshval = ((i * err_range) / DITH_SIZE)+cr_values[j];
  93.  
  94.       for (k=cr_values[j]; k<cr_values[j+1]; k++) {
  95.     if (k > threshval) *cmark++ = ((j+1) * CB_RANGE);
  96.     else *cmark++ = (j * CB_RANGE);
  97.       }
  98.     }
  99.  
  100.     for (j=cr_values[CR_RANGE-1]; j<256; j++) {
  101.       *cmark++ = (CR_RANGE-1)*(CB_RANGE);
  102.     }
  103.   }
  104.  
  105.   for (i=0; i<DITH_SIZE; i++) {
  106.     cmark = cb_darrays[i] = (unsigned char *) malloc(256);
  107.  
  108.     for (j=0; j<cb_values[0]; j++) {
  109.       *cmark++ = 0;
  110.     }
  111.  
  112.     for (j=0; j<(CB_RANGE-1); j++) {
  113.       err_range = cb_values[j+1] - cb_values[j];
  114.       threshval = ((i * err_range) / DITH_SIZE)+cb_values[j];
  115.  
  116.       for (k=cb_values[j]; k<cb_values[j+1]; k++) {
  117.     if (k > threshval) *cmark++ = j+1;
  118.     else *cmark++ = j;
  119.       }
  120.     }
  121.  
  122.     for (j=cb_values[CB_RANGE-1]; j<256; j++) {
  123.       *cmark++ = CB_RANGE-1;
  124.     }
  125.   }
  126. }
  127.  
  128. /*
  129.  *--------------------------------------------------------------
  130.  *
  131.  * OrderedDitherImage --
  132.  *
  133.  *    Dithers an image using an ordered dither.
  134.  *    Assumptions made:
  135.  *      1) The color space is allocated y:cr:cb = 8:4:4
  136.  *      2) The spatial resolution of y:cr:cb is 4:1:1
  137.  *      The channels are dithered based on the standard
  138.  *      ordered dither pattern for a 4x4 area. 
  139.  *
  140.  * Results:
  141.  *    None.
  142.  *
  143.  * Side effects:
  144.  *    None.
  145.  *
  146.  *--------------------------------------------------------------
  147.  */
  148. void
  149. OrderedDitherImage (lum, cr, cb, out, h, w)
  150.     unsigned char *lum;
  151.     unsigned char *cr;
  152.     unsigned char *cb;
  153.     unsigned char *out;
  154.     int w, h;
  155. {
  156.   unsigned char *l, *r, *b, *o1, *o2;
  157.   unsigned char *l2;
  158.   unsigned char L, R, B;
  159.   int i, j;
  160.  
  161.   l = lum;
  162.   l2 = lum+w;
  163.   r = cr;
  164.   b = cb;
  165.   o1 = out;
  166.   o2 = out+w;
  167.  
  168.   for (i=0; i<h; i+=4) {
  169.  
  170.     for (j=0; j<w; j+=8) {
  171.  
  172.       R = r[0]; B = b[0];
  173.  
  174.       L = l[0];
  175.       o1[0] = pixel[(l_darrays[0][L] + cr_darrays[0][R] + cb_darrays[0][B])];
  176.       L = l[1];
  177.       o1[1] = pixel[(l_darrays[8][L] + cr_darrays[8][R] + cb_darrays[8][B])];
  178.       L = l2[0];
  179.       o2[0] = pixel[(l_darrays[12][L] + cr_darrays[12][R] + cb_darrays[12][B])];
  180.       L = l2[1];
  181.       o2[1] = pixel[(l_darrays[4][L] + cr_darrays[4][R] + cb_darrays[4][B])];
  182.  
  183.       R = r[1]; B = b[1];
  184.  
  185.       L = l[2];
  186.       o1[2] = pixel[(l_darrays[2][L] + cr_darrays[2][R] + cb_darrays[2][B])];
  187.       L = l[3];
  188.       o1[3] = pixel[(l_darrays[10][L] + cr_darrays[10][R] + cb_darrays[10][B])];
  189.       L = l2[2];
  190.       o2[2] = pixel[(l_darrays[14][L] + cr_darrays[14][R] + cb_darrays[14][B])];
  191.       L = l2[3];
  192.       o2[3] = pixel[(l_darrays[6][L] + cr_darrays[6][R] + cb_darrays[6][B])];
  193.  
  194.       R = r[2]; B = b[2];
  195.  
  196.       L = l[4];
  197.       o1[4] = pixel[(l_darrays[0][L] + cr_darrays[0][R] + cb_darrays[0][B])];
  198.       L = l[5];
  199.       o1[5] = pixel[(l_darrays[8][L] + cr_darrays[8][R] + cb_darrays[8][B])];
  200.       L = l2[4];
  201.       o2[4] = pixel[(l_darrays[12][L] + cr_darrays[12][R] + cb_darrays[12][B])];
  202.       L = l2[5];
  203.       o2[5] = pixel[(l_darrays[4][L] + cr_darrays[4][R] + cb_darrays[4][B])];
  204.  
  205.       R = r[3]; B = b[3];
  206.  
  207.       L = l[6];
  208.       o1[6] = pixel[(l_darrays[2][L] + cr_darrays[2][R] + cb_darrays[2][B])];
  209.       L = l[7];
  210.       o1[7] = pixel[(l_darrays[10][L] + cr_darrays[10][R] + cb_darrays[10][B])];
  211.       L = l2[6];
  212.       o2[6] = pixel[(l_darrays[14][L] + cr_darrays[14][R] + cb_darrays[14][B])];
  213.       L = l2[7];
  214.       o2[7] = pixel[(l_darrays[6][L] + cr_darrays[6][R] + cb_darrays[6][B])];
  215.  
  216.       l += 8;
  217.       l2 += 8;
  218.       r += 4;
  219.       b += 4;
  220.       o1 += 8;
  221.       o2 += 8;
  222.     }
  223.  
  224.     l += w; l2 += w;
  225.     o1 += w; o2 += w;
  226.  
  227.     for (j=0; j<w; j+=8) {
  228.  
  229.       R = r[0]; B = b[0];
  230.  
  231.       L = l[0];
  232.       o1[0] = pixel[(l_darrays[3][L] + cr_darrays[3][R] + cb_darrays[3][B])];
  233.       L = l[1];
  234.       o1[1] = pixel[(l_darrays[11][L] + cr_darrays[11][R] + cb_darrays[11][B])];
  235.       L = l2[0];
  236.       o2[0] = pixel[(l_darrays[15][L] + cr_darrays[15][R] + cb_darrays[15][B])];
  237.       L = l2[1];
  238.       o2[1] = pixel[(l_darrays[7][L] + cr_darrays[7][R] + cb_darrays[7][B])];
  239.  
  240.       R = r[1]; B = b[1];
  241.  
  242.       L = l[2];
  243.       o1[2] = pixel[(l_darrays[1][L] + cr_darrays[1][R] + cb_darrays[1][B])];
  244.       L = l[3];
  245.       o1[3] = pixel[(l_darrays[9][L] + cr_darrays[9][R] + cb_darrays[9][B])];
  246.       L = l2[2];
  247.       o2[2] = pixel[(l_darrays[13][L] + cr_darrays[13][R] + cb_darrays[13][B])];
  248.       L = l2[3];
  249.       o2[3] = pixel[(l_darrays[5][L] + cr_darrays[5][R] + cb_darrays[5][B])];
  250.  
  251.       R = r[2]; B = b[2];
  252.  
  253.       L = l[4];
  254.       o1[4] = pixel[(l_darrays[3][L] + cr_darrays[3][R] + cb_darrays[3][B])];
  255.       L = l[5];
  256.       o1[5] = pixel[(l_darrays[11][L] + cr_darrays[11][R] + cb_darrays[11][B])];
  257.       L = l2[4];
  258.       o2[4] = pixel[(l_darrays[15][L] + cr_darrays[15][R] + cb_darrays[15][B])];
  259.       L = l2[5];
  260.       o2[5] = pixel[(l_darrays[7][L] + cr_darrays[7][R] + cb_darrays[7][B])];
  261.  
  262.       R = r[3]; B = b[3];
  263.  
  264.       L = l[6];
  265.       o1[6] = pixel[(l_darrays[1][L] + cr_darrays[1][R] + cb_darrays[1][B])];
  266.       L = l[7];
  267.       o1[7] = pixel[(l_darrays[9][L] + cr_darrays[9][R] + cb_darrays[9][B])];
  268.       L = l2[6];
  269.       o2[6] = pixel[(l_darrays[13][L] + cr_darrays[13][R] + cb_darrays[13][B])];
  270.       L = l2[7];
  271.       o2[7] = pixel[(l_darrays[5][L] + cr_darrays[5][R] + cb_darrays[5][B])];
  272.  
  273.       l += 8;
  274.       l2 += 8;
  275.       r += 4;
  276.       b += 4;
  277.       o1 += 8;
  278.       o2 += 8;
  279.     }
  280.  
  281.     l += w; l2 += w;
  282.     o1 += w; o2 += w;
  283.   }
  284. }
  285.  
  286.  
  287.   
  288.